package com.shushan.pages;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.shushan.base.BaseActivity;
import com.shushan.base.ScreenManager;
import com.shushan.home.adapter.GuidePagerAdapter;
import com.shushan.login.LoginOrRegisterActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.CirclePageIndicator;
import com.shushan.util.ControlUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GuideActivity
 * @Desciption: //引导页
 * @author: zhangshihao
 * @date: 2018-04-03
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener,View.OnClickListener
,View.OnTouchListener{

    private ViewPager vpGuide;
    private CirclePageIndicator cpiGuide;
    private Button btnSkip,btnEnter;
    private GuidePagerAdapter guidePagerAdapter;
    private List<Integer> imgResIdList;
    private List<Integer> textResIdList;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    private float startX;
    private float endX;
    private int currentItem;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true,this);
        initView();
        initWhite();
    }

    private void initView(){
        vpGuide = findViewById(R.id.vp_guide);
        cpiGuide = findViewById(R.id.cpi_guide);
        btnSkip = findViewById(R.id.btn_skip);
        btnEnter = findViewById(R.id.btn_enter);

        vpGuide.addOnPageChangeListener(this);
        btnSkip.setOnClickListener(this);
        btnEnter.setOnClickListener(this);
    }

    private void initWhite() {
        String[] contacts = getResources().getStringArray(R.array.contact_pack_name_array);
        for (String contact : contacts){
            if(!TextUtils.isEmpty(contact)){
                ControlUtil.addPackage(true,contact);
            }
        }

        String[] mms = getResources().getStringArray(R.array.mms_pack_name_array);
        for (String mm : mms){
            if(!TextUtils.isEmpty(mm)){
                ControlUtil.addPackage(true,mm);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_enter:
            case R.id.btn_skip:
                startActivity(LoginOrRegisterActivity.class);
                onBackAction();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        initViewPager();
    }

    private void initViewPager(){
        imgResIdList = new ArrayList<>();
        imgResIdList.add(R.drawable.guide_pager_1);
        imgResIdList.add(R.drawable.guide_pager_2);
        imgResIdList.add(R.drawable.guide_pager_3);
        textResIdList = new ArrayList<>();
        textResIdList.add(R.drawable.guide_pager_text_1);
        textResIdList.add(R.drawable.guide_pager_text_2);
        textResIdList.add(R.drawable.guide_pager_text_3);
        guidePagerAdapter = new GuidePagerAdapter(this,imgResIdList,textResIdList);
        vpGuide.setOnTouchListener(this);
        vpGuide.setAdapter(guidePagerAdapter);
        cpiGuide.setViewPager(vpGuide);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX=event.getX();
                break;
            case MotionEvent.ACTION_UP:
                endX=event.getX();
                WindowManager windowManager= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

                //获取屏幕的宽度
                Point size = new Point();
                windowManager.getDefaultDisplay().getSize(size);
                int width=size.x;

                //首先要确定的是，是否到了最后一页，然后判断是否向左滑动，并且滑动距离是否符合，我这里的判断距离是屏幕宽度的4分之一（这里可以适当控制）
                if(currentItem==(imgResIdList.size()-1)&&startX-endX>=(width/4)){
                    startActivity(LoginOrRegisterActivity.class);
                    onBackAction();
                    overridePendingTransition(R.anim.slide_in_right,R.anim.slide_in_left);//这部分代码是切换Activity时的动画，看起来就不会很生硬
                }
                break;
        }
        return false;
    }

    @Override
    public void onPageSelected(int position) {
        currentItem = position;
        if(position == (imgResIdList.size() - 1)){
            btnEnter.setVisibility(View.VISIBLE);
            btnSkip.setVisibility(View.GONE);
        }else{
            btnEnter.setVisibility(View.GONE);
            btnSkip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //do noting
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // do nothing
    }
}
